package com.tools.module.base.service.impl;

import com.tools.common.dynamicquery.DynamicQuery;
import com.tools.common.model.PageBean;
import com.tools.common.model.Result;
import com.tools.common.util.DateUtils;
import com.tools.module.base.repository.BaseGroupRepository;
import com.tools.module.base.entity.BaseGroup;
import com.tools.module.base.service.BaseGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BaseGroupServiceImpl implements BaseGroupService {

    @Autowired
    private DynamicQuery dynamicQuery;
    @Autowired
    private BaseGroupRepository groupRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(BaseGroup group) {

        group.setUpdateTime(DateUtils.getTimestamp());
        groupRepository.saveAndFlush(group);
        return Result.ok("保存成功");
    }

    @Override
    public Result get(Long id) {
        BaseGroup group = groupRepository.getOne(id);
        return Result.ok(group);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Long id) {
        groupRepository.deleteById(id);
        return Result.ok("删除成功");
    }

    @Override
    public Result list(BaseGroup group) {
        String nativeSql = "SELECT COUNT(*) FROM base_group where group_id = 0";
        Long count = dynamicQuery.nativeQueryCount(nativeSql);
        PageBean<BaseGroup> data = new PageBean<>();
        if (count > 0) {
            nativeSql = "SELECT * FROM base_group where group_id =0 ORDER BY id seq";
            Pageable pageable = PageRequest.of(group.getPageNo(), group.getPageSize());
            List<BaseGroup> list = dynamicQuery.nativeQueryPagingList(BaseGroup.class, pageable, nativeSql);
            data = new PageBean(list, count);
        }
        return Result.ok(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateSeq(BaseGroup group) {
        String nativeSql = "update base_group set seq = seq +1 where seq >= ? and group_id = ?";
        dynamicQuery.nativeExecuteUpdate(nativeSql, group.getSeq(), group.getGroupId());
        return Result.ok("修改成功");
    }

    @Override
    public Long queryBySeq(BaseGroup group) {
        String nativeSql = "SELECT COUNT(*) FROM base_group where  group_id =" + group.getGroupId() + "and seq = " + group.getSeq();
        Long count = dynamicQuery.nativeQueryCount(nativeSql);

        return count;
    }

    @Override
    public Result queryPeopleByGroupId(BaseGroup group) {

        String nativeSql = "SELECT COUNT(*) FROM base_group g inner join base_people p  on g.people_id = p.id" +
                "where   group_id != 0";
        if (group.getGroupId() != null) {
            nativeSql = "SELECT COUNT(*) FROM base_group g inner join base_people p  on g.people_id = p.id where  group_id =" + group.getGroupId() + "order by g.group_id,g.seq";
        }

        Long count = dynamicQuery.nativeQueryCount(nativeSql);
        PageBean<BaseGroup> data = new PageBean<>();
        if (count > 0) {
            nativeSql = "SELECT * FROM base_group g inner join base_people p  on g.people_id = p.id where  group_id != 0 order by g.group_id,g.seq";
            if (group.getGroupId() != null) {
                nativeSql = "SELECT * FROM base_group g inner join base_people p  on g.people_id = p.id where  group_id =" + group.getGroupId() + "order by g.group_id,g.seq";
            }
            Pageable pageable = PageRequest.of(group.getPageNo(), group.getPageSize());
            List<BaseGroup> list = dynamicQuery.nativeQueryPagingList(BaseGroup.class, pageable, nativeSql);
            data = new PageBean(list, count);
        }
        return Result.ok(data);
    }
}
